package com.xqh.study.netflixexampleuser.web;

import com.google.common.collect.Maps;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xqh.study.netflixexampleuser.dao.CustomerRepository;
import com.xqh.study.netflixexampleuser.domain.Customer;
import com.xqh.study.netflixexampleuser.feign.OrderClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * @ClassName CustomerResource
 * @Description TODO
 * @Author xuqianghui
 * @Date 2019/12/30 21:41
 * @Version 1.0
 */
@RequestMapping("/api/customer")
@RestController
@Slf4j
public class CustomerResource {

    @Autowired
    private CustomerRepository customerRepository;

    @Autowired
    private OrderClient orderClient;

    @PostConstruct
    public void init(){
        Customer customer = new Customer();
        customer.setUsername("xqh");
        customer.setPassword("123456");
        customer.setRole("admin");
        customerRepository.save(customer);
    }

    @PostMapping("")
    @HystrixCommand
    public Customer create(@RequestBody Customer customer){
        return customerRepository.save(customer);
    }


    @GetMapping("")
    @HystrixCommand
    public Object getAll(){
        return customerRepository.findAll();
    }


    @GetMapping("getMyInfo")
    public Map<String, Object> getMyInfo(){
        Map<String, Object> retMap = Maps.newHashMap();
        retMap.put("customer", customerRepository.findOneByUsername("xqh"));
        retMap.put("order", orderClient.getOrderById(1L));
        return retMap;
    }



}
